package com.lanren;

import com.lanren.utils.DateUtils;
import com.lanren.utils.JwtToken;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

public class TokenTest {

    public static void main(String[] args) throws Exception {
        // 生成Token
        String token = JwtToken.createToken("lanren", "android");

//        String testPassword1 = "12345q";
//        String testPassword2 = "12345q";
//
//        System.out.println("testPassword1 16=" + MD5Utils.MD5Encode16(testPassword1));
//        System.out.println("testPassword2 16=" + MD5Utils.MD5Encode16(testPassword2));
//
//        System.out.println("testPassword1 32=" + MD5Utils.MD5Encode32(testPassword1));
//        System.out.println("testPassword2 32=" + MD5Utils.MD5Encode32(testPassword2));
//
//        System.out.println("testPassword1 64=" + MD5Utils.MD5Encode64(testPassword1));
//        System.out.println("testPassword2 64=" + MD5Utils.MD5Encode64(testPassword2));
//        System.out.println("判断:" + MD5Utils.MD5verify16(testPassword1, MD5Utils.MD5Encode16(testPassword2), false));

        String time1 = "21:14:59";
        String time2 = "21:50:59";
        System.out.println(DateUtils.checkedBiggerThanNowTime(time1));
        System.out.println(DateUtils.checkedBiggerThanNowTime(time2));
        System.out.println(DateUtils.checkedBiggerThanNowTime(time2) && !DateUtils.checkedBiggerThanNowTime(time1));

        Calendar cal=Calendar.getInstance();
        cal.setTimeInMillis(Objects.requireNonNull(JwtToken.getIssuedAt(token)).getTime());
        cal.add(Calendar.DATE,1);//这里改为1
        cal.set(Calendar.HOUR_OF_DAY, 4);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Date time=cal.getTime();
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time));
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(DateUtils.getTomorrowTime(4,0,0)));


        // 显示Token
//        System.out.println("Token:" + token);
//        System.out.println("Token size:" + token.getBytes().length);
//        System.out.println("MD5Utils:" + MD5Utils.generate(token));
//        System.out.println("MD5Utils size:" + MD5Utils.generate(token).getBytes().length);
//        System.out.println("MD5Utils size:" + MD5Utils.verify(token,MD5Utils.generate(token)));



       /* Date dateClient = new Date();
        Thread.sleep(2000);

        System.out.println("第1次校验Token，正常通过------------------------------------------------");

        JwtToken.verifyRequest(dateClient, token, 3000);

        System.out.println("userId=" + JwtToken.getUserId(token));
        System.out.println("userOs=" + JwtToken.getUserOs(token));
        System.out.println("ExpiresAt=" + JwtToken.getExpiresAt(token));
        System.out.println("IssuedAt=" + JwtToken.getIssuedAt(token));

        // 使用过期后的Token进行校验
        System.out.println("第2次校验Token，Token错误------------------------------------------------");
        String tokenExpire = "22";

        JwtToken.verifyRequest(dateClient, tokenExpire, 500);

        System.out.println("第3次校验Token，Token正确，但http访问时间超时------------------------------------------------");

        Thread.sleep(1000);
        JwtToken.verifyRequest(dateClient, token, 500);

        System.out.println("第4次校验Token，Token正确，http访问也不超时，但Token已过期（10秒钟）------------------------------------------------");

        Thread.sleep(5000);
        JwtToken.verifyRequest(dateClient, token, 3000);

        System.out.println("测试结束");*/
    }
}
